/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernate.dao;

//import mill.hibernate.dto.DeliveryInstruction;
import java.util.ArrayList;
import mill.hibernate.dto.DeliveryInstruction_mill;
import mill.hibernate.dto.QualityReport_mill;
import mill.hibernate.dto.WeightNote_mill;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import trader.hibernate.dto.*;

/**
 *
 * @author kiendn
 */
public class DeliveryInstructionDao {

    private Session session = null;

    public DeliveryInstructionDao() {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        session = sf.openSession();
    }

    public boolean Is_Mediate(int trader_di_id, int warehouse_id) {
        try {
            session.getTransaction().begin();

            Criteria crit = session.createCriteria(TraderdiMilldi.class);

            crit.add(Restrictions.eq("traderDiId", trader_di_id));
            crit.add(Restrictions.eq("warehouseId", warehouse_id));

            TraderdiMilldi mediate = (TraderdiMilldi) crit.uniqueResult();

            session.flush();
            session.getTransaction().commit();

            if (mediate != null) {
                return true;
            }
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return false;
    }

    public boolean save_to_mediate(TraderdiMilldi mediate) {
        try {
            session.getTransaction().begin();
            session.save(mediate);

            session.flush();
            session.getTransaction().commit();

            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return false;
    }

    public TraderdiMilldi getMediate(int trader_id, int warehouse_id) {
        try {
            session.getTransaction().begin();

            Criteria crit = session.createCriteria(TraderdiMilldi.class);

            crit.add(Restrictions.eq("traderDiId", trader_id));
            crit.add(Restrictions.eq("warehouseId", warehouse_id));

            TraderdiMilldi mediate = (TraderdiMilldi) crit.uniqueResult();

            session.flush();
            session.getTransaction().commit();

            return mediate;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
    
    public int getTraderDiId(int mill_id){
        try {
            session.getTransaction().begin();

            Criteria crit = session.createCriteria(TraderdiMilldi.class);

            crit.add(Restrictions.eq("millDiId", mill_id));
            //crit.add(Restrictions.eq("warehouseId", warehouse_id));

            TraderdiMilldi mediate = (TraderdiMilldi) crit.uniqueResult();

            session.flush();
            session.getTransaction().commit();

            if (mediate != null){
                return mediate.getTraderDiId();
            }
            //return di;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return -1;
    }

    public DeliveryInstruction getDIById(int id) {
        try {
            session.getTransaction().begin();

            Criteria crit = session.createCriteria(DeliveryInstruction.class);

            crit.add(Restrictions.eq("id", id));
            //crit.add(Restrictions.eq("warehouseId", warehouse_id));

            DeliveryInstruction di = (DeliveryInstruction) crit.uniqueResult();

            session.flush();
            session.getTransaction().commit();

            return di;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    public int getDI_mill_id(int trader_id) {
        try {
            session.getTransaction().begin();

            Criteria crit = session.createCriteria(TraderdiMilldi.class);

            crit.add(Restrictions.eq("traderDiId", trader_id));
            //crit.add(Restrictions.eq("warehouseId", warehouse_id));

            TraderdiMilldi di = (TraderdiMilldi) crit.uniqueResult();

            session.flush();
            session.getTransaction().commit();

            return di.getMillDiId();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return -1;
    }

    public int updateDI(DeliveryInstruction di) {
        try {
            session.getTransaction().begin();
            session.update(di);

            session.flush();
            session.getTransaction().commit();

            return di.getId();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return 0;
    }

    public int saveDI(DeliveryInstruction di) {
        try {
            session.getTransaction().begin();
            session.save(di);

            session.flush();
            session.getTransaction().commit();

            return di.getId();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return 0;
    }

    public int getContractId(String ref_number) {
        try {
            session.getTransaction().begin();

            Criteria crit = session.createCriteria(Contracts.class);

            crit.add(Restrictions.eq("refNumber", ref_number));
            //crit.add(Restrictions.eq("warehouseId", warehouse_id));

            Contracts ct = (Contracts) crit.uniqueResult();

            session.flush();
            session.getTransaction().commit();

            return ct.getId();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return -1;
    }

    public String getNewWnRef() {
        try {
            session.getTransaction().begin();

            Criteria crit = session.createCriteria(WeightNote.class);

            crit.setProjection(Projections.max("refNumber"));
            String ref = (String) crit.uniqueResult();
            session.flush();
            session.getTransaction().commit();
            //DeliveryInstruction_mill DI = (DeliveryInstruction_mill)crit.uniqueResult();
            if (ref != null && !ref.equals("")) {
                return getNextWnRefNumber(ref);
            }
            return "SWCVN-WN-IM-12-00001";

        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        //return false;
        return "";
    }

    public String getNewQrRef() {
        try {
            session.getTransaction().begin();

            Criteria crit = session.createCriteria(QualityReport.class);

            crit.setProjection(Projections.max("refNumber"));
            String ref = (String) crit.uniqueResult();
            session.flush();
            session.getTransaction().commit();
            //DeliveryInstruction_mill DI = (DeliveryInstruction_mill)crit.uniqueResult();
            if (ref != null && !ref.equals("")) {
                return getNextWnRefNumber(ref);
            }

            return "SWCVN-QR-IM-12-00001";
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        //return false;
        return "";
    }

    public String getNextWnRefNumber(String ref_number) {
        String result = "";

        String[] new_string = ref_number.split("-");
        if (new_string.length == 5) {
            int n = Integer.parseInt(new_string[4]);
            int lengthOfNum = ("" + (n + 1)).length();
            switch (lengthOfNum) {
                case 1:
                    new_string[4] = "0000" + (n + 1);
                    break;
                case 2:
                    new_string[4] = "000" + (n + 1);
                    break;
                case 3:
                    new_string[4] = "00" + (n + 1);
                    break;
                case 4:
                    new_string[4] = "0" + (n + 1);
                    break;
                case 5:
                    new_string[4] = "" + (n + 1);
                    break;
                default:
                    new_string[4] = "" + (n + 1);
            }
            result = new_string[0] + "-" + new_string[1] + "-" + new_string[2] + "-" + new_string[3] + "-" + new_string[4];
        }
        return result;
    }

    public String getNextRefNumber(String ref_number) {
        String result = "";

        String[] new_string = ref_number.split("-");
        if (new_string.length == 4) {
            int n = Integer.parseInt(new_string[3]);
            int lengthOfNum = ("" + (n + 1)).length();
            switch (lengthOfNum) {
                case 1:
                    new_string[3] = "0000" + (n + 1);
                    break;
                case 2:
                    new_string[3] = "000" + (n + 1);
                    break;
                case 3:
                    new_string[3] = "00" + (n + 1);
                    break;
                case 4:
                    new_string[3] = "0" + (n + 1);
                    break;
                case 5:
                    new_string[3] = "" + (n + 1);
                    break;
                default:
                    new_string[3] = "" + (n + 1);
            }
            result = new_string[0] + "-" + new_string[1] + "-" + new_string[2] + "-" + new_string[3];
        }
        return result;
    }

    public String getNewDIRef() {
        try {
            session.getTransaction().begin();

            Criteria crit = session.createCriteria(DeliveryInstruction.class);

            crit.setProjection(Projections.max("refNumber"));
            String ref = (String) crit.uniqueResult();
            session.flush();
            session.getTransaction().commit();
            if (ref != null && !ref.equals("")) {
                return getNextRefNumber(ref);
            } else {
                return "SWCVN-DI-12-00001";
            }

        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        //return false;
        return "";
    }

    private int getNewQR_id() {
        try {
            int new_id = 0;
            session.getTransaction().begin();

            Criteria crit = session.createCriteria(QualityReport.class);

            crit.setProjection(Projections.max("id"));
            if (crit.uniqueResult() != null) {
                new_id = (Integer) crit.uniqueResult();
            }
            session.flush();
            session.getTransaction().commit();
            //DeliveryInstruction_mill DI = (DeliveryInstruction_mill)crit.uniqueResult();

            return (new_id + 1);

        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return -1;
    }

    public int saveQr(QualityReport qr) {
        try {

            int new_id = getNewQR_id();
            if (new_id > 0) {
                qr.setId(new_id);
                qr.setRefNumber(getNewQrRef());
                session.getTransaction().begin();
                session.save(qr);

                session.flush();
                session.getTransaction().commit();

                return new_id;
            }

        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }

        return -1;
    }

    public void updateQr(QualityReport qr) {
        try {
            session.getTransaction().begin();
            session.update(qr);

            session.flush();
            session.getTransaction().commit();


        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }

    }

    public void updateWn(WeightNote wn) {
        try {
            session.getTransaction().begin();
            session.update(wn);

            session.flush();
            session.getTransaction().commit();


        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }

    }

    private int getNewWN_id() {
        try {
            int new_id = 0;
            session.getTransaction().begin();

            Criteria crit = session.createCriteria(WeightNote.class);

            crit.setProjection(Projections.max("id"));
            if (crit.uniqueResult() != null) {
                new_id = (Integer) crit.uniqueResult();
            }
            session.flush();
            session.getTransaction().commit();
            //DeliveryInstruction_mill DI = (DeliveryInstruction_mill)crit.uniqueResult();

            return (new_id + 1);

        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return -1;
    }

    public int saveWn(WeightNote wn) {
        try {

            int new_id = getNewWN_id();
            if (new_id > 0) {
                wn.setId(new_id);

                session.getTransaction().begin();
                session.save(wn);

                session.flush();
                session.getTransaction().commit();

                return new_id;
            }

        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return -1;
    }

    public int getWnId(WeightNote_mill wn) {
        try {
            session.getTransaction().begin();

            Criteria crit = session.createCriteria(Mediate.class);

            crit.add(Restrictions.eq("millId", wn.getId()));
            crit.add(Restrictions.eq("type", "WN"));
            //crit.add(Restrictions.eq("warehouseId", warehouse_id));

            Mediate result = (Mediate) crit.uniqueResult();

            session.flush();
            session.getTransaction().commit();

            if (result != null) {
                return result.getTraderId();
            }
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return -1;
    }

    public int getQrId(QualityReport_mill qr_mill) {
        try {
            session.getTransaction().begin();

            Criteria crit = session.createCriteria(Mediate.class);

            crit.add(Restrictions.eq("millId", qr_mill.getId()));
            crit.add(Restrictions.eq("type", "QR"));
            //crit.add(Restrictions.eq("warehouseId", warehouse_id));

            Mediate result = (Mediate) crit.uniqueResult();

            session.flush();
            session.getTransaction().commit();

            if (result != null) {
                return result.getTraderId();
            }
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return -1;
    }

    public boolean save_to_QrWn(Mediate mediate) {
        try {
            session.getTransaction().begin();
            session.save(mediate);

            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return false;
    }
    
    public ArrayList<Contracts> getPurchaseContracts(){
        try {
            session.getTransaction().begin();
            
            Criteria crit = session.createCriteria(Contracts.class);
            char type = 'P';
            crit.add(Restrictions.eq("type", type));
            
            ArrayList<Contracts> list = (ArrayList<Contracts>)crit.list();
            
            session.flush();
            session.getTransaction().commit();
            
            return list;
            
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
}
